package com.angel.bo.common.context;

import com.angel.bo.common.constant.CommonConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:
 * @Description:
 * @Date: 16:56 2018/1/25
 */
public class CommonContextHandler {


    private static volatile ThreadLocal<Map<String, Object>> THREADLOCAL = new ThreadLocal<>();

    public static void set(String key, Object value) {
        Map<String, Object> map = THREADLOCAL.get();
        if (map == null) {
            map = new HashMap<>(10);
            THREADLOCAL.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key){
        Map<String, Object> map = THREADLOCAL.get();
        if (map == null) {
            map = new HashMap<>(10);
            THREADLOCAL.set(map);
        }
        return map.get(key);
    }

    public static void remove(){
        THREADLOCAL.remove();
    }

    public static void setToken(String token){
        set(CommonConstants.CONTEXT_KEY_USER_TOKEN,token);
    }
}
